﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using Toolbox.NETMF.NET;
using Toolbox.NETMF.Hardware;
using ReefinDotNet.OneWireDevices;
using ReefinDotNet.i2c;


namespace ReefinDotNet
{
    public partial class Program
    {
        static readonly SNTP_Client TimeClient = new SNTP_Client(new IntegratedSocket("nist1-lnk.binary.net", 123));
        static DateTime startup;
        //Set your OneWire Bus Pin here- Remember the 4.7k pull up from 5v to this pin!
        //static DS18B20 temp1 = new DS18B20(Pins.GPIO_PIN_D10);
        private double[] temp = new double[1];
        private static bool network = true;
        //_lastFire, _doneFilling and WaitTime are all used in the ATO Control area to make some smart choices on when to pump water in andwhen to stop. _ATOEnabled is a bool to turn off the ATO system on demand via WCP
        private static DateTime _lastFire = DateTime.Now.AddSeconds(-30);
        private static bool _doneFilling = true;
        private static readonly TimeSpan WaitTime = new TimeSpan(0, 0, 0, 1);
        private static bool _ATOEnabled = true;
        static readonly Adafruit_PWMServoDriver mainLights = new Adafruit_PWMServoDriver(0x40, 100);
        static readonly InputPort SumpLevel1 = new InterruptPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
        static readonly OutputPort ATO = new OutputPort(Pins.GPIO_PIN_D0, true);
       
        private double[] caclLights = new double[5];
        public static void Main()
        {
            mainLights.Reset();
            #region TimeSet
            try
            {
                TimeClient.Synchronize();
                Utility.SetLocalTime(DateTime.Now.AddHours(-6));
                startup = DateTime.Now;
            }
            catch
            {
                Debug.Print("Couldn't Set Time");
                network = false;
                mainLights.Override = true;
                mainLights.fadeTo(100, 100, 100, 100, 100, 5);
            }
            #endregion
            Program Reefin = new Program();
            //If the Sump is Already Low on startup, lets fill it!
            if (SumpLevel1.Read() == false) // check for low water on start up, if so fire the event manually.
            {
                waterLowEvent(0, 0, DateTime.Now);
            }
            //set what happens on interrupt
            SumpLevel1.OnInterrupt += new NativeEventHandler(waterLowEvent);
            #region Start Threads
            //WebServer.cs - Service requests for XML
                Thread WebServer = new Thread(start: Reefin.webServer);
                WebServer.Start();
            //Temperature.cs - Check Temp - Controls the Heater
                Thread Temp = new Thread(start: Reefin.MonitorTemp);
               // Temp.Start();
            //Calculate Lights - Sets the PWM output for the lights.
                Thread CalculateLights = new Thread(start: Reefin.calculateLights);
                CalculateLights.Start();

            #endregion
        }

    }
}
